//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.telnet.command;

import com.comprosoft.telnet.configuration.ServerProperties;
import com.comprosoft.telnet.io.IOHandler;
import com.comprosoft.telnet.server.TelnetServer;
import com.comprosoft.utils.ProcessUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import org.apache.commons.lang.StringEscapeUtils;

public class SystemCommandHandler extends Command {
    private static final Logger logger = Logger.getLogger("SystemCommandHandler");
    volatile boolean ifError = false;

    public SystemCommandHandler(IOHandler io, TelnetServer telnetServer) {
        super(io, "sys", telnetServer);
    }

    public String getOsCmd() {
        Properties props = System.getProperties();
        String osName = props.getProperty("os.name");
        if (osName.toLowerCase().contains("linux")) {
            return "/bin/sh -c ";
        } else if (osName.toLowerCase().contains("windows")) {
            return "cmd /c ";
        } else {
            throw new RuntimeException("服务器不是linux|windows操作系统");
        }
    }

    public CommandResult handleCommand(String[] args) {
        long startTime = System.currentTimeMillis();
        StringBuffer commandStr = new StringBuffer();
        int i = 0;
        String[] var6 = args;
        int var7 = args.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String arg = var6[var8];
            if (i != 0) {
                commandStr.append(arg).append(" ");
            }

            ++i;
        }

        logger.log(Level.INFO, "sys execute:" + commandStr.toString());

        try {
            String jarPath = Matcher.quoteReplacement(ServerProperties.JAR_PATH.getValue());
            String command = StringEscapeUtils.unescapeHtml(commandStr.toString());
            ProcessBuilder pb = new ProcessBuilder((this.getOsCmd() + command).split(" "));
            final Process process = pb.start();
            long pid = ProcessUtils.getPid(process);
            final IOHandler ioHandler = super.getIOHandler();
            this.getTelnetServer().getLongCustomProcessMap().put(pid, new CustomProcess(process, ioHandler));

            try {
                ioHandler.WriteLine("{pid:" + pid + "}");
                Thread busiThread = new Thread(new Runnable() {
                    public void run() {
                        BufferedReader br = null;

                        try {
                            br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
                            String line = null;

                            while((line = br.readLine()) != null) {
                                ioHandler.WriteLine(line);
                            }
                        } catch (Exception var11) {
                        } finally {
                            if (br != null) {
                                try {
                                    br.close();
                                } catch (Exception var10) {
                                }
                            }

                        }

                    }
                });
                busiThread.start();
                Thread errorThread = new Thread(new Runnable() {
                    public void run() {
                        BufferedReader brError = null;

                        try {
                            brError = new BufferedReader(new InputStreamReader(process.getErrorStream(), "GBK"));

                            for(String lineError = null; (lineError = brError.readLine()) != null; SystemCommandHandler.this.ifError = true) {
                                ioHandler.WriteLine(lineError);
                                System.out.println(lineError);
                            }
                        } catch (Exception var11) {
                        } finally {
                            if (brError != null) {
                                try {
                                    brError.close();
                                } catch (Exception var10) {
                                }
                            }

                        }

                    }
                });
                errorThread.start();
                process.waitFor(100L, TimeUnit.MILLISECONDS);
            } finally {
                if (process != null) {
                }

            }

            return this.ifError ? CommandResult.COMMAND_EXIT : CommandResult.COMMAND_OK;
        } catch (Exception var19) {
            var19.printStackTrace();
            return CommandResult.COMMAND_EXIT;
        }
    }

    public static void main(String[] args) throws Exception {
        for(int i = 0; i < 20; ++i) {
            String subCommond = null;
            if (i % 2 == 0) {
                subCommond = "start";
            } else {
                subCommond = "stop";
            }

            ProcessBuilder pb = new ProcessBuilder(("cmd /c net " + subCommond + " telcat").split(" "));
            pb.redirectErrorStream(true);
            Process process = null;
            BufferedReader br = null;

            try {
                process = pb.start();
                br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
                String line = null;

                while((line = br.readLine()) != null) {
                    System.out.println(line);
                }

                process.waitFor();
            } finally {
                if (br != null) {
                    br.close();
                }

                if (process != null) {
                    process.exitValue();
                    process.destroy();
                    Runtime.getRuntime().gc();
                }

            }
        }

    }
}
